In [24]:
import math
nums = range(3, 1001)
primes = [2]
for i in nums:
is_prime = True
for j in range(2, int(math.sqrt(i)+1)):
if i % j == 0:
is_prime = False
break
if is_prime:
primes.append(i)
print primes, len(primes)
In [10]:
nums = range(2, 1001)
for i, num in enumerate(nums):
if num != 0:
to_zero = i + num
while to_zero < len(nums):
nums[to_zero] = 0
to_zero += num
primes = [p for p in nums if p!=0]
print primes, len(primes)
In [26]:
fibo = [1, 1]
for i in range(2, 15):
fibo.append(fibo[i-1] + fibo[i-2])
print fibo[-1]
In [29]:
#bubble sort
arr = [9 , 1, 2, 15, 23, 7, 8, 6, 4, 3, 17]
count = len(arr)
for i in range(count):
for j in range(1, count - i):
if arr[j-1] > arr[j]:
temp = arr[j]
arr[j] = arr[j-1]
arr[j-1] = temp
print arr
print arr
In [31]:
#selection sort
arr = [9, 1, 2, 15, 23, 7, 8, 6, 4, 3, 17]
count = len(arr)
for i in range(count):
minimum = arr[i]
minimum_idx = i
for j in range(i + 1, count):
if minimum > arr[j]:
minimum = arr[j]
minimum_idx = j
temp = arr[i]
arr[i] = arr[minimum_idx]
arr[minimum_idx] = temp
print arr
print arr
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: